package com.kk.gray.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfiguration implements RequestInterceptor {

    // 配置所有的feig 都加请求头
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            // 兼容定时任务中，没有request的场景
            return;
        }
        HttpServletRequest request = attributes.getRequest ( );
        requestTemplate.header ("userId", request.getHeader ("userId"));
    }
}
